//
//  YDAOptionEditingController.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/26.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "YDAOptionEditingController.h"
#import "YDAOptionEditingTableHeaderView.h"
#import "YDAOptionEditingCell.h"
#import "YDAOptionalStockModel.h"


@interface YDAOptionEditingController ()<UITableViewDelegate , UITableViewDataSource , SYAOptionEditingCellDelegate>

//tableView
@property (nonatomic , weak) UITableView *tableView;


//置顶股票数组
@property (nonatomic , strong) NSMutableArray *topStockArray;

@property (nonatomic , copy) NSString *sourceString;


@end

@implementation YDAOptionEditingController

-(NSMutableArray *)topStockArray
{
    if (_topStockArray == nil) {
        _topStockArray = [NSMutableArray array];
    }
    return _topStockArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"编辑我的自选";
    self.view.backgroundColor = [UIColor colorWithHexString:mainGray];
    
    [self setUpNavButton];
    
    [self tableView];
    
    if (self.optionalStockArray.count) {
        self.sourceString = [self.optionalStockArray componentsJoinedByString:@","];
    }
    
}
-(void)setUpNavButton
{
    UIBarButtonItem *leftItemBtn = [UIBarButtonItem imageWithNormalImage:[UIImage imageNamed:@"zixuan-wancheng"] hightImage:[UIImage imageNamed:@"zixuan-wancheng"] addTarget:self action:@selector(leftItemBtnClick:)];
    self.navigationItem.leftBarButtonItem = leftItemBtn;
    
    UIBarButtonItem *rightItemBtn = [UIBarButtonItem imageWithNormalImage:[UIImage imageNamed:@"zixuan-shanchu"] hightImage:[UIImage imageNamed:@"zixuan-shanchu"] addTarget:self action:@selector(rightItemBtnClick:)];
    self.navigationItem.rightBarButtonItem = rightItemBtn;
}
-(void)leftItemBtnClick:(UIButton *)button
{
    if (self.topStockArray.count == 0) {
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }else{
        
        NSMutableArray *array = [NSMutableArray array];
        for (YDAOptionalStockModel *model in self.topStockArray) {
            [array addObject:model.code];
        }
        
        NSString *IDs = [array componentsJoinedByString:@","];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:IDs forKey:@"stockCodes"];
        
        [BaseNetRequest sessionRequestWithparameters:dict method:POSTMethod requstType:TopOptionalStock WithSuccessBlock:^(NSDictionary *resultDict) {
            DDLog(@"%@",resultDict)
            DDLog(@"%@",resultDict[@"message"])
            
            if ([resultDict[@"code"] integerValue] == 200) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }else{
                
                [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"置顶未成功~" isExistNav:YES];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        } failBlock:^(NSError *error) {
            DDLog(@"%@",error)
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"置顶未成功~" isExistNav:YES];
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }
    
    
}
-(void)rightItemBtnClick:(UIButton *)button
{
    DDLog(@"点击删除")
    NSMutableArray *selectedStockArray = [NSMutableArray array];
    
    for (YDAOptionalStockModel *obj in self.optionalStockArray) {
        if (obj.editingIsSelected) {
            [selectedStockArray addObject:obj.code];
        }
    }
    
    if (selectedStockArray.count == 0) {
        
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"请先选择股票" isExistNav:YES];
    }else{
        NSString *IDs = [selectedStockArray componentsJoinedByString:@","];
        
        [[SYProgressHUD sharedInstance] showMBHudOnlyLoading];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:IDs forKey:@"stockCodes"];
        [BaseNetRequest sessionRequestWithparameters:dict method:POSTMethod requstType:DeleteOptionalStock WithSuccessBlock:^(NSDictionary *resultDict) {
            [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
            
            if ([resultDict[@"code"] integerValue] == 200) {
                
                [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"删除自选成功~" isExistNav:YES];
                
                NSMutableArray *array = [NSMutableArray arrayWithArray:self.optionalStockArray];
                for (YDAOptionalStockModel *obj in array) {
                    if (obj.editingIsSelected) {
                        [self.optionalStockArray removeObject:obj];
                    }
                }
                [self.tableView reloadData];
            }else{
                [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:resultDict[@"message"] isExistNav:YES];
            }
        } failBlock:^(NSError *error) {
            [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"网络出错" isExistNav:YES];
        }];
    }
}
/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame;
        if (isPhoneX) {
            frame = CGRectMake(0, 0, LKScreenW, LKScreenH - 88);
        }else{
            frame = CGRectMake(0, 0, LKScreenW, LKScreenH - 64);
        }
        UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = NumberColor(247, 247, 247);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        _tableView = tableView;
    }
    return _tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.optionalStockArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return isPhoneX ? rateX812(58) : rate750(58);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return isPhoneX ? rateX812(32) : rate750(32);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YDAOptionEditingTableHeaderView *headerView = [YDAOptionEditingTableHeaderView headerViewWithTableView:tableView];

    return headerView;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDAOptionEditingCell *cell = [YDAOptionEditingCell cellWithTableView:tableView];
    cell.delegate = self;
    YDAOptionalStockModel *model = self.optionalStockArray[indexPath.row];
    cell.model = model;
    return cell;
}

/*******PZOptionalEditingTableCellDelegate*******/
-(void)aOptionEditingCellDidClickSelected:(YDAOptionEditingCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    YDAOptionalStockModel *model = self.optionalStockArray[indexPath.row];
    model.editingIsSelected = !model.editingIsSelected;
    [self.tableView reloadData];
}

-(void)aOptionEditingCellDidClickTopButton:(YDAOptionEditingCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    if (indexPath.row == 0) {
        return;
    }
    
    YDAOptionalStockModel *model = self.optionalStockArray[indexPath.row];
    [self.optionalStockArray removeObject:model];
    [self.optionalStockArray insertObject:model atIndex:0];
    [self.tableView reloadData];
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.topStockArray];
    [self.topStockArray removeAllObjects];
    if (array.count) {
        if ([array containsObject:model]) {
            [array removeObject:model];
            [array insertObject:model atIndex:0];
        }else{
            [array insertObject:model atIndex:0];
        }
    }else{
        [array addObject:model];
    }
    
    [self.topStockArray addObjectsFromArray:array];
    
    
    
}
@end
